package cn.xsshome.mvcdo.util;

import cn.xsshome.mvcdo.pojo.ai.baidu.po.WordsResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("ALL")
public class WordConvertCodeTool {
    private static Logger logger = LoggerFactory.getLogger(WordConvertCodeTool.class);
    
    public static void main(String[] args) {
        WordConvertCodeTool demo = new WordConvertCodeTool();
        //String json = "[{\"words\":\"开始运行\",\"location\":{\"top\":47,\"left\":153,\"width\":140,\"height\":35}},{\"words\":\"重复直到\",\"location\":{\"top\":125,\"left\":98,\"width\":138,\"height\":35}},{\"words\":\"向日葵\",\"location\":{\"top\":117,\"left\":412,\"width\":113,\"height\":40}},{\"words\":\"向前\",\"location\":{\"top\":190,\"left\":294,\"width\":85,\"height\":35}},{\"words\":\"如果\",\"location\":{\"top\":271,\"left\":214,\"width\":70,\"height\":34}},{\"words\":\"左边有路\",\"location\":{\"top\":263,\"left\":503,\"width\":149,\"height\":41}},{\"words\":\"左转\",\"location\":{\"top\":331,\"left\":403,\"width\":150,\"height\":48}},{\"words\":\"如果结束\",\"location\":{\"top\":411,\"left\":216,\"width\":139,\"height\":36}},{\"words\":\"重复结束\",\"location\":{\"top\":477,\"left\":107,\"width\":139,\"height\":36}}]";
        //String json = "[{\"words\":\"开始运行\",\"location\":{\"height\":36,\"width\":134,\"left\":80,\"top\":21}},{\"words\":\"重复\",\"location\":{\"height\":32,\"width\":67,\"left\":20,\"top\":86}},{\"words\":\"5\",\"location\":{\"height\":41,\"width\":31,\"left\":212,\"top\":88}},{\"words\":\"重复\",\"location\":{\"height\":31,\"width\":65,\"left\":15,\"top\":178}},{\"words\":\"向东\",\"location\":{\"height\":33,\"width\":76,\"left\":179,\"top\":161}},{\"words\":\"结束\",\"location\":{\"height\":33,\"width\":65,\"left\":15,\"top\":212}},{\"words\":\"采花\",\"location\":{\"height\":34,\"width\":81,\"left\":93,\"top\":282}},{\"words\":\"向北\",\"location\":{\"height\":35,\"width\":81,\"left\":106,\"top\":360}},{\"words\":\"制作蜜\",\"location\":{\"height\":42,\"width\":159,\"left\":99,\"top\":424}}]";
        //String json ="[{\"words\":\"开始运行\",\"location\":{\"height\":34,\"width\":124,\"left\":71,\"top\":23}},{\"words\":\"重复\",\"location\":{\"height\":32,\"width\":61,\"left\":14,\"top\":85}},{\"words\":\"3\",\"location\":{\"height\":35,\"width\":26,\"left\":196,\"top\":86}},{\"words\":\"向西\",\"location\":{\"height\":31,\"width\":75,\"left\":164,\"top\":158}},{\"words\":\"←\",\"location\":{\"height\":56,\"width\":77,\"left\":249,\"top\":144}},{\"words\":\"向西\",\"location\":{\"height\":33,\"width\":74,\"left\":167,\"top\":224}},{\"words\":\"←\",\"location\":{\"height\":52,\"width\":72,\"left\":252,\"top\":213}},{\"words\":\"采花\",\"location\":{\"height\":37,\"width\":80,\"left\":157,\"top\":286}},{\"words\":\"重复\",\"location\":{\"height\":31,\"width\":61,\"left\":15,\"top\":381}},{\"words\":\"向南\",\"location\":{\"height\":31,\"width\":74,\"left\":169,\"top\":365}},{\"words\":\"结束\",\"location\":{\"height\":31,\"width\":62,\"left\":15,\"top\":414}},{\"words\":\"向南\",\"location\":{\"height\":32,\"width\":75,\"left\":97,\"top\":490}},{\"words\":\"重复\",\"location\":{\"height\":32,\"width\":61,\"left\":21,\"top\":554}},{\"words\":\"3\",\"location\":{\"height\":49,\"width\":61,\"left\":210,\"top\":547}},{\"words\":\"重复\",\"location\":{\"height\":30,\"width\":62,\"left\":14,\"top\":649}},{\"words\":\"制作蜜C\",\"location\":{\"height\":43,\"width\":154,\"left\":160,\"top\":621}},{\"words\":\"结束\",\"location\":{\"height\":31,\"width\":62,\"left\":14,\"top\":681}}]";

        //String json = "[{\"words\":\"第一课1\",\"location\":{\"top\":34,\"left\":113,\"width\":267,\"height\":59}},{\"words\":\"开始运行\",\"location\":{\"top\":121,\"left\":112,\"width\":183,\"height\":44}},{\"words\":\"向右\",\"location\":{\"top\":217,\"left\":184,\"width\":110,\"height\":44}},{\"words\":\"向右\",\"location\":{\"top\":321,\"left\":183,\"width\":111,\"height\":45}}]";
        //String json = "[{\"words\":\"第四课2\",\"location\":{\"top\":49,\"left\":94,\"width\":200,\"height\":43}},{\"words\":\"开始运行\",\"location\":{\"top\":108,\"left\":79,\"width\":138,\"height\":34}},{\"words\":\"重复\",\"location\":{\"top\":192,\"left\":78,\"width\":71,\"height\":36}},{\"words\":\"5\",\"location\":{\"top\":188,\"left\":370,\"width\":28,\"height\":38}},{\"words\":\"向东\",\"location\":{\"top\":261,\"left\":257,\"width\":85,\"height\":35}},{\"words\":\"向西\",\"location\":{\"top\":261,\"left\":257,\"width\":85,\"height\":35}},{\"words\":\"重复结束\",\"location\":{\"top\":327,\"left\":60,\"width\":138,\"height\":34}}]";
        //String json = "[{\"words\":\"挑战八6\"},{\"words\":\"开始运行\"},{\"words\":\"重复次数\"},{\"words\":\"4\"},{\"words\":\"向右\"},{\"words\":\"捡宝石\"},{\"words\":\"重复结束\"},{\"words\":\"重复次数\"},{\"words\":\"4\"},{\"words\":\"向上\"},{\"words\":\"重复结束\"}]";
        //String json = "[{\"words\":\"挑战九\"},{\"words\":\"1\"},{\"words\":\"开始运行\"},{\"words\":\"重复次数\"},{\"words\":\"3\"},{\"words\":\"向前\"},{\"words\":\"重复结束\"},{\"words\":\"如果\"},{\"words\":\"是花朵\"},{\"words\":\"采花粉\"},{\"words\":\"如果结束\"}]";
        //String json = "[{\"words\":\"挑战四4\"},{\"words\":\"开始运行\"},{\"words\":\"重复次数3\"},{\"words\":\"向下\"},{\"words\":\"↓\"},{\"words\":\"重复结束\"},{\"words\":\"重复次数4\"},{\"words\":\"向右\"},{\"words\":\"重复结束\"}]";
        //String json = "[{\"words\":\"挑战四6\"},{\"words\":\"开始运行\"},{\"words\":\"重复次数\"},{\"words\":\"向下\"},{\"words\":\"重复结束\"},{\"words\":\"重复次数3\"},{\"words\":\"向右\"},{\"words\":\"重复结束\"},{\"words\":\"重复次数\"},{\"words\":\"向上\"},{\"words\":\"重复结束\"}]";
        String json = "[{\"words\":\"挑战八10\"},{\"words\":\"开始运行\"},{\"words\":\"重复次数2\"},{\"words\":\"重复次数6\"},{\"words\":\"向前\"},{\"words\":\"捡宝石\"},{\"words\":\"重复结束\"},{\"words\":\"向下\"},{\"words\":\"↓\"},{\"words\":\"重复结束\"}]";

        JSONArray list = JSONObject.parseArray(json);
        List<WordsResult> tmpList = list.toJavaList(WordsResult.class);
        List<WordsResult> filterList = filterWordsList(tmpList);
        
        StringBuffer result = new StringBuffer();
        //convertXMLCodeOne(filterList, result);
        //convertXMLCodeFour(filterList,result);
        convertXMLCodeEight(filterList, result);
        //convertXMLCodeNine(filterList, result);
        
        System.out.println("==========result:"+result);
    }

    /**
     * 过滤仅需要的关键词
     * @param list
     * @return
     */
    public static List<WordsResult> filterWordsList(List<WordsResult> list){
        logger.info("===cg===filterWordsList:start:{}",JSONObject.toJSONString(list));
        List<WordsResult> resultList = new ArrayList<WordsResult>();
        if(list == null || list.size() ==0) return resultList;
        for(int i = 0;i < list.size();i ++){
            WordsResult dto = list.get(i);
            String curWord = dto.getWords().trim();
            WordsResult newDTO = new WordsResult();
            newDTO.setWords(curWord.replaceAll(">","").replaceAll("\\)",""));
            if(curWord.contains("开始运行") 
                    || curWord.contains("东") || curWord.contains("南")
                    || curWord.contains("西") || curWord.contains("北") 
                    || curWord.contains("前") || curWord.contains("课") || curWord.contains("战")
                    || curWord.contains("左") || curWord.contains("右")
                    || curWord.contains("上") || curWord.contains("下")
                    || curWord.contains("重复")|| curWord.contains("如果")
                    || curWord.contains("结束") || curWord.contains("制")
                    || curWord.contains("直到") || curWord.contains("如果")
                    || curWord.contains("玉米") || curWord.contains("做蜂")
                    || curWord.contains("捡宝石") || curWord.contains("花")
                    || curWord.contains("采")
                    ) {
                resultList.add(newDTO);
            }else if(HasDigit(curWord)) {
                resultList.add(newDTO);
            }else{
                resultList.add(newDTO);
            }
        }
        logger.info("===cg===filterWordsList:zero{}",JSONObject.toJSONString(resultList));
        List<WordsResult> oneList =  mergeList(resultList);
        List<WordsResult> twoList = makeupList(oneList);
        logger.info("===cg===filterWordsList==twoList:{}",JSONObject.toJSONString(twoList));
        return twoList;
    }

    private static List<WordsResult> makeupList(List<WordsResult> oneList){
        logger.info("===cg===makeupList==:{}",JSONObject.toJSONString(oneList));
        List<WordsResult> results = new ArrayList<WordsResult>();
        for(int i = 0;i < oneList.size(); i++){
            WordsResult tmpWR = oneList.get(i);
            String word = tmpWR.getWords().trim();
            System.out.println(i+"cg===word:"+word+"===="+HasDigit(word));
            if(word.contains("重复次数")){//重复次数3
                results.add(tmpWR);
                if(HasDigit(word)){
                    WordsResult digitWR = new WordsResult();
                    digitWR.setWords(word.substring("重复次数".length(),word.length()));
                    results.add(digitWR);
                }else if(!HasDigit(oneList.get(i +1).getWords().trim())){//如果下一个值不是数字，则AI识别数字失败，默认值为四
                    WordsResult digitWR = new WordsResult();
                    digitWR.setWords("4");
                    results.add(digitWR);
                }

            }else{
                results.add(tmpWR);
            }

        }
        return results;
    }
    private static List<WordsResult> mergeList(List<WordsResult> list){
        if(list.size() < 2) return list;

        String word0 = list.get(0).getWords().trim();
        String word1 = list.get(1).getWords().trim();
        if(HasDigit(word0) ) {//含有课程挑战一1
            return list;
        }else if(HasDigit(word1)){
            WordsResult words = new WordsResult();
            words.setWords(word0 + word1);
            list.remove(0);
            list.add(0,words);
            list.remove(1);
            return list;
        }
        return list;
    }
    public static StringBuffer convertXMLCodeOne(List<WordsResult> listWords,StringBuffer result){
        logger.info("convertXMLCodeOne===cg===start=list:{}",JSONObject.toJSONString(listWords));
        List<String> directionList = new ArrayList<String>();
        if(listWords == null || listWords.size() == 0) return result;

        result.append("<xml>");

        StringBuffer xmlCode = convertMyXMLCode(listWords);
        result.append(xmlCode);

        result.append("</xml>");
        return result;
    }

    /**
     * 查找字符串里与指定字符串相同的个数
     * @param source
     * @param target
     * @return
     */
    public static int search(String source,String target){
        int n =0;
        int idx = 0;
        idx = source.indexOf(target);
        while(idx != -1){
            n ++;
            idx = source.indexOf(target,idx +1);
        }
        return n;
    }

    public static void convertXMLCodeFour(List<WordsResult> list, StringBuffer result) {
        logger.info("convertXMLCodeFour===cg===start=list:{}",JSONObject.toJSONString(list));
        List<String> loopCodeList = new ArrayList<String>();
        boolean loopFlag = false;
        Integer loopCount = 0;
        Integer firstNext = 0;
        List<String> directionList = new ArrayList<String>();
        if(list == null || list.size() == 0) return ;

        result.append("<xml>");
        int i = 0;
        do{
            WordsResult dto = list.get(i);
            String curWord = dto.getWords();
            System.out.println(i+"==="+curWord);
            i = i + 1;

            if(curWord.contains("开始") || curWord.contains("课") || curWord.contains("挑战") ) continue;
            if(curWord.contains("北") || curWord.contains("上")){
                directionList.add("maze_moveNorth");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("下")){
                directionList.add("maze_moveSouth");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("左")){
                directionList.add("maze_moveWest");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("右")){
                directionList.add("maze_moveEast");
                continue;
            }
            if(curWord.contains("花")){//采花粉
                directionList.add("maze_nectar");
                continue;
            }
            if(curWord.contains("制作蜜")){
                directionList.add("maze_honey");
                continue;
            }
            if(curWord.contains("重复") && loopFlag == false){
                loopCount = loopCount + 1;
                directionList.add("controls_repeat_simplified_dropdown=CG="+loopCount);
                loopFlag = true;
                continue;
            }

            if(loopFlag){
                StringBuffer loopResult = new StringBuffer();
                int loop = 1;
                try {
                    loop = Integer.parseInt(curWord);
                } catch (NumberFormatException e) {
                    loop = 1;
                    logger.error("convertXMLCodeFour===",e);
                }
                //获取循环内部的脚本信息
                List<WordsResult> listLoop = new ArrayList<WordsResult>();
                String tmpWord="";
                do{
                    WordsResult tmp = list.get(i);
                    tmpWord = tmp.getWords();
                    logger.info("i={},tmpWord==={}",i,tmpWord);
                    i = i + 1;//偏移至循环体内部的具体执行步骤
                    if(tmpWord.contains("复结束")){
                        loopFlag =false;
                        break;
                    }

                    listLoop.add(tmp);
                }while((!tmpWord.contains("结束") && i < list.size()));

                loopResult.append("<title name=\"TIMES\" config=\"2-6\">").append(loop).append("</title>").append("<statement name=\"DO\">");
                loopResult.append(convertMyXMLCode(listLoop));
                loopResult.append("</statement>");

                loopCodeList.add(loopResult.toString());


                continue;
            }


        }while(i < list.size());

        for(int k = 0;k < directionList.size(); k ++ ){
            String typeWord = directionList.get(k).trim();

            if( k > 0){
                result.append("<next>");
                firstNext = firstNext + 1;
            }
            if(typeWord.contains("controls_repeat_simplified_dropdown")){
                result.append("<block type=\"").append("controls_repeat_simplified_dropdown").append("\">");
                //重复 xml code makeup
                int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                result.append(loopCodeList.get(tmpCount - 1));
            }else{
                result.append("<block type=\"").append(typeWord).append("\">");
            }

        }
        result.append("</block>");
        for(int k = 0; k < firstNext;k ++){
            result.append("</next></block>");
        }

        result.append("</xml>");
    }


    public static void convertXMLCodeSeven(List<WordsResult> list, StringBuffer result) {
            logger.info("convertXMLCodeSeven===cg===start=list:{}",JSONObject.toJSONString(list));
            List<String> loopCodeList = new ArrayList<String>();
            boolean loopFlag = false;
            Integer loopCount = 0;
            Integer firstNext = 0;
            List<String> directionList = new ArrayList<String>();
            if(list == null || list.size() == 0) return ;

            result.append("<xml>");
            int i = 0;
            do{
                WordsResult dto = list.get(i);
                String curWord = dto.getWords();
                System.out.println(i+"==="+curWord);
                i = i + 1;

                if(curWord.contains("开始") || curWord.contains("课") || curWord.contains("挑战") ) continue;
                if(curWord.contains("北") || curWord.contains("上")){
                    directionList.add("maze_moveNorth");
                    continue;
                }
                if(curWord.contains("南") || curWord.contains("下")){
                    directionList.add("maze_moveSouth");
                    continue;
                }
                if(curWord.contains("西")  || curWord.contains("左")){
                    directionList.add("maze_moveWest");
                    continue;
                }
                if(curWord.contains("东")  || curWord.contains("右")){
                    directionList.add("maze_moveEast");
                    continue;
                }
                if(curWord.contains("摘玉米")){
                    directionList.add("harvester_corn");
                    continue;
                }
                if(curWord.contains("重复") && loopFlag == false){
                    loopCount = loopCount + 1;
                    directionList.add("controls_repeat_simplified_dropdown=CG="+loopCount);
                    loopFlag = true;
                    continue;
                }

                if(loopFlag){
                    StringBuffer loopResult = new StringBuffer();
                    int loop = 1;
                    try {
                        loop = Integer.parseInt(curWord);
                    } catch (NumberFormatException e) {
                        loop = 1;
                        logger.error("convertXMLCodeFour===",e);
                    }
                    //获取循环内部的脚本信息
                    List<WordsResult> listLoop = new ArrayList<WordsResult>();
                    String tmpWord="";
                    do{
                        WordsResult tmp = list.get(i);
                        tmpWord = tmp.getWords();
                        logger.info("i={},tmpWord==={}",i,tmpWord);
                        i = i + 1;//偏移至循环体内部的具体执行步骤
                        if(tmpWord.contains("重复结束")){
                            loopFlag =false;
                            break;
                        }

                        listLoop.add(tmp);
                    }while((!tmpWord.contains("结束") && i < list.size()));

                    loopResult.append("<title name=\"TIMES\" config=\"2-6\">").append(loop).append("</title>").append("<statement name=\"DO\">");
                    loopResult.append(convertMyXMLCode(listLoop));
                    loopResult.append("</statement>");

                    loopCodeList.add(loopResult.toString());


                    continue;
                }


            }while(i < list.size());

            for(int k = 0;k < directionList.size(); k ++ ){
                String typeWord = directionList.get(k).trim();

                if( k > 0){
                    result.append("<next>");
                    firstNext = firstNext + 1;
                }
                if(typeWord.contains("controls_repeat_simplified_dropdown")){
                    result.append("<block type=\"").append("controls_repeat_simplified_dropdown").append("\">");
                    //重复 xml code makeup
                    int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                    result.append(loopCodeList.get(tmpCount - 1));
                }else{
                    result.append("<block type=\"").append(typeWord).append("\">");
                }

            }
            result.append("</block>");
            for(int k = 0; k < firstNext;k ++){
                result.append("</next></block>");
            }

            result.append("</xml>");
        }
    /**
     * 组装 单步操作类 xml 格式字符串
     * @param listLoop
     * @return
     */
    private static StringBuffer convertMyXMLCode(List<WordsResult> listLoop) {
        StringBuffer sb = new StringBuffer();
        List<String> directionList = new ArrayList<String>();
        int i =0;
        do{
            WordsResult dto = listLoop.get(i);
            String curWord = dto.getWords();
            i = i + 1;
            if(curWord.contains("北") || curWord.contains("上")){
                directionList.add("maze_moveNorth");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("下")){
                directionList.add("maze_moveSouth");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("左")){
                directionList.add("maze_moveWest");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("右")){
                directionList.add("maze_moveEast");
                continue;
            }
            if(curWord.contains("花")){
                directionList.add("maze_nectar");
                continue;
            }
            if(curWord.contains("制作蜜") || curWord.contains("做蜂")){
                directionList.add("maze_honey");
                continue;
            }
            if(curWord.contains("玉米")){//摘玉米
                directionList.add("harvester_corn");
                continue;
            }
            if(curWord.contains("宝石")){//宝石
                directionList.add("collector_collect");
                continue;
            }

        }while(i < listLoop.size());
        //组装 xml 格式
        for(int k = 0;k < directionList.size(); k ++ ){
            if( k > 0) sb.append("<next>");
            sb.append("<block type=\"").append(directionList.get(k)).append("\">");
        }
        //计算next个数
        int nextCount = search(sb.toString(),"next");
        sb.append("</block>");
        for(int k = 0; k < nextCount;k ++){
            sb.append("</next></block>");
        }

        return sb;
    }

    public static void convertXMLCodeAuto(List<WordsResult> list, StringBuffer result) {
        logger.info("convertXMLCodeFour===cg===start=list:{}",JSONObject.toJSONString(list));
        List<String> loopCodeList = new ArrayList<String>();
        boolean loopFlag = false;
        Integer loopCount = 0;
        Integer firstNext = 0;
        List<String> directionList = new ArrayList<String>();
        if(list == null || list.size() == 0) return ;

        result.append("<xml>");
        int i = 0;
        do{
            WordsResult dto = list.get(i);
            String curWord = dto.getWords();
            System.out.println(i+"==="+curWord);
            i = i + 1;

            if(curWord.contains("开始") || curWord.contains("课") || curWord.contains("挑战") ) continue;
            if(curWord.contains("北") || curWord.contains("向上") || curWord.contains("向前")){
                directionList.add("simple_move_up");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("向下")){
                directionList.add("simple_move_down");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("向左")){
                directionList.add("simple_move_left");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("向右")){
                directionList.add("simple_move_right");
                continue;
            }

            if(curWord.contains("左上")){
                directionList.add("simple_move_up_left");
                continue;
            }
            if(curWord.contains("右上")){
                directionList.add("simple_move_up_right");
                continue;
            }
            if(curWord.contains("左下")){
                directionList.add("simple_move_down_left");
                continue;
            }
            if(curWord.contains("右下")){
                directionList.add("simple_move_down_right");
                continue;
            }

            if(curWord.contains("重复") && loopFlag == false){
                loopCount = loopCount + 1;
                directionList.add("controls_repeat_simplified_dropdown=CG="+loopCount);
                loopFlag = true;
                continue;
            }

            if(loopFlag){
                StringBuffer loopResult = new StringBuffer();
                int loop = 1;
                try {
                    loop = Integer.parseInt(curWord);
                } catch (NumberFormatException e) {
                    loop = 1;
                    logger.error("convertXMLCodeFour===",e);
                }
                //获取循环内部的脚本信息
                List<WordsResult> listLoop = new ArrayList<WordsResult>();
                String tmpWord="";
                do{
                    WordsResult tmp = list.get(i);
                    tmpWord = tmp.getWords();
                    logger.info("i={},tmpWord==={}",i,tmpWord);
                    i = i + 1;//偏移至循环体内部的具体执行步骤
                    if(tmpWord.contains("重复结束")){
                        loopFlag =false;
                        break;
                    }

                    listLoop.add(tmp);
                }while((!tmpWord.contains("结束") && i < list.size()));

                loopResult.append("<title name=\"TIMES\" config=\"2-6\">").append(loop).append("</title>").append("<statement name=\"DO\">");
                loopResult.append(convertMyXMLCodeByAuto(listLoop));
                loopResult.append("</statement>");

                loopCodeList.add(loopResult.toString());


                continue;
            }


        }while(i < list.size());

        for(int k = 0;k < directionList.size(); k ++ ){
            String typeWord = directionList.get(k).trim();

            if( k > 0){
                result.append("<next>");
                firstNext = firstNext + 1;
            }
            if(typeWord.contains("controls_repeat_simplified_dropdown")){
                result.append("<block type=\"").append("controls_repeat_simplified_dropdown").append("\">");
                //重复 xml code makeup
                int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                result.append(loopCodeList.get(tmpCount - 1));
            }else{
                result.append("<block type=\"").append(typeWord).append("\">");
            }

        }
        result.append("</block>");
        for(int k = 0; k < firstNext;k ++){
            result.append("</next></block>");
        }

        result.append("</xml>");
    }

    private static StringBuffer convertMyXMLCodeByAuto(List<WordsResult> listLoop) {
        StringBuffer sb = new StringBuffer();
        List<String> directionList = new ArrayList<String>();
        int i =0;
        do{
            WordsResult dto = listLoop.get(i);
            String curWord = dto.getWords();
            i = i + 1;
            if(curWord.contains("北") || curWord.contains("向上")){
                directionList.add("simple_move_up");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("向下")){
                directionList.add("simple_move_down");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("向左")){
                directionList.add("simple_move_left");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("向右")){
                directionList.add("simple_move_right");
                continue;
            }

            if(curWord.contains("左上")){
                directionList.add("simple_move_up_left");
                continue;
            }
            if(curWord.contains("右上")){
                directionList.add("simple_move_up_right");
                continue;
            }
            if(curWord.contains("左下")){
                directionList.add("simple_move_down_left");
                continue;
            }
            if(curWord.contains("右下")){
                directionList.add("simple_move_down_right");
                continue;
            }

        }while(i < listLoop.size());
        //组装 xml 格式
        for(int k = 0;k < directionList.size(); k ++ ){
            if( k > 0) sb.append("<next>");
            sb.append("<block type=\"").append(directionList.get(k)).append("\">");
        }
        //计算next个数
        int nextCount = search(sb.toString(),"next");
        sb.append("</block>");
        for(int k = 0; k < nextCount;k ++){
            sb.append("</next></block>");
        }

        return sb;
    }

    public static void convertXMLCodeEight(List<WordsResult> list, StringBuffer result) {
        logger.info("convertXMLCodeEight===cg===start=list:{}",JSONObject.toJSONString(list));
        List<String> loopCodeList = new ArrayList<String>();
        boolean loopFlag = false;
        Integer loopCount = 0;
        Integer firstNext = 0;
        List<String> directionList = new ArrayList<String>();
        if(list == null || list.size() == 0) return ;

        result.append("<xml>");
        int i = 0;
        do{
            WordsResult dto = list.get(i);
            String curWord = dto.getWords();
            System.out.println(i+"==="+curWord);
            i = i + 1;

            if(curWord.contains("开始") || curWord.contains("课") || curWord.contains("挑战") ) continue;
            if(curWord.contains("北") || curWord.contains("上")){
                directionList.add("maze_moveNorth");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("下")){
                directionList.add("maze_moveSouth");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("左")){
                directionList.add("maze_moveWest");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("右")){
                directionList.add("maze_moveEast");
                continue;
            }
            if(curWord.contains("宝石")){
                directionList.add("collector_collect");
                continue;
            }
            if(curWord.contains("重复") && loopFlag == false){
                loopCount = loopCount + 1;
                directionList.add("controls_repeat_simplified_dropdown=CG="+loopCount);
                loopFlag = true;
                continue;
            }

            if(loopFlag){
                StringBuffer loopResult = new StringBuffer();
                int loop = 1;
                try {
                    loop = Integer.parseInt(curWord);
                } catch (NumberFormatException e) {
                    loop = 1;
                    logger.error("convertXMLCodeFour===",e);
                }
                //获取循环内部的脚本信息
                List<WordsResult> listLoop = new ArrayList<WordsResult>();
                String tmpWord="";
                do{
                    WordsResult tmp = list.get(i);
                    tmpWord = tmp.getWords();
                    logger.info("i={},tmpWord==={}",i,tmpWord);
                    i = i + 1;//偏移至循环体内部的具体执行步骤
                    if(tmpWord.contains("复结束")){
                        loopFlag =false;
                        break;
                    }

                    listLoop.add(tmp);
                }while((!tmpWord.contains("结束") && i < list.size()));

                loopResult.append("<title name=\"TIMES\" config=\"2-6\">").append(loop).append("</title>").append("<statement name=\"DO\">");
                loopResult.append(convertMyXMLCode(listLoop));
                loopResult.append("</statement>");

                loopCodeList.add(loopResult.toString());


                continue;
            }


        }while(i < list.size());

        for(int k = 0;k < directionList.size(); k ++ ){
            String typeWord = directionList.get(k).trim();

            if( k > 0){
                result.append("<next>");
                firstNext = firstNext + 1;
            }
            if(typeWord.contains("controls_repeat_simplified_dropdown")){
                result.append("<block type=\"").append("controls_repeat_simplified_dropdown").append("\">");
                //重复 xml code makeup
                int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                result.append(loopCodeList.get(tmpCount - 1));
            }else{
                result.append("<block type=\"").append(typeWord).append("\">");
            }

        }
        result.append("</block>");
        for(int k = 0; k < firstNext;k ++){
            result.append("</next></block>");
        }

        result.append("</xml>");
    }

    public static void convertXMLCodeNine(List<WordsResult> list, StringBuffer result) {
        logger.info("convertXMLCodeNine===cg===start=list:{}",JSONObject.toJSONString(list));
        List<String> loopCodeList = new ArrayList<String>();
        List<String> ifCodeList = new ArrayList<String>();
        boolean loopFlag = false;
        boolean ifFlag = false;
        Integer loopCount = 0;
        Integer ifCount = 0;
        Integer firstNext = 0;
        List<String> directionList = new ArrayList<String>();
        if(list == null || list.size() == 0) return ;

        result.append("<xml>");
        int i = 0;
        do{
            WordsResult dto = list.get(i);
            String curWord = dto.getWords();
            String curifCMD = "";
            System.out.println(i+"==="+curWord);
            i = i + 1;

            if(curWord.contains("开始") || curWord.contains("课") || curWord.contains("挑战") ) continue;
            if(curWord.contains("北") || curWord.contains("上")){
                directionList.add("maze_moveNorth");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("下")){
                directionList.add("maze_moveSouth");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("左")){
                directionList.add("maze_moveWest");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("右")){
                directionList.add("maze_moveEast");
                continue;
            }
            if(curWord.contains("花")){
                directionList.add("maze_nectar");
                continue;
            }
            if(curWord.contains("左转")){//向左转90度
                directionList.add("turnLeft");
                continue;
            }
            if(curWord.contains("右转")){//向右转90度
                directionList.add("turnRight");
                continue;
            }

            if(curWord.contains("前")){//向前移动
                directionList.add("maze_moveForward");
                continue;
            }

            if(curWord.contains("重复") && loopFlag == false){
                loopCount = loopCount + 1;
                directionList.add("controls_repeat=CG="+loopCount);
                loopFlag = true;
                continue;
            }

            if(loopFlag){
                StringBuffer loopResult = new StringBuffer();
                int loop = 1;
                try {
                    loop = Integer.parseInt(curWord);
                } catch (NumberFormatException e) {
                    loop = 1;
                    logger.error("convertXMLCodeFour===",e);
                }
                //获取循环内部的脚本信息
                List<WordsResult> listLoop = new ArrayList<WordsResult>();
                String tmpWord="";
                do{
                    WordsResult tmp = list.get(i);
                    tmpWord = tmp.getWords();
                    logger.info("i={},tmpWord==={}",i,tmpWord);
                    i = i + 1;//偏移至循环体内部的具体执行步骤
                    if(tmpWord.contains("复结束")){
                        loopFlag =false;
                        break;
                    }
                    listLoop.add(tmp);
                }while((!tmpWord.contains("结束") && i < list.size()));

                loopResult.append("<title name=\"TIMES\">").append(loop).append("</title>").append("<statement name=\"DO\">");
                loopResult.append(convertMyXMLCodeNine(listLoop));
                loopResult.append("</statement>");

                loopCodeList.add(loopResult.toString());
                continue;
            }

            if(curWord.contains("如果") && ifFlag == false) {
                ifCount = ifCount + 1;
                directionList.add("bee_ifFlower=CG=" + ifCount);
                ifFlag = true;
                continue;
            }
            if(ifFlag){
                StringBuffer ifResult = new StringBuffer();
                List<WordsResult> listLoop = new ArrayList<WordsResult>();
                String tmpWord="";

                if(curWord.contains("花朵") || curWord.contains("在花")){//是花朵，在花里 逻辑判断不跳出循环
                    //directionList.add("atFlower");
                    curifCMD = "atFlower";
                }
                do{
                    WordsResult tmp = list.get(i);
                    tmpWord = tmp.getWords();
                    logger.info("i={},tmpWord==={}",i,tmpWord);
                    i = i + 1;//偏移至循环体内部的具体执行步骤
                    if(tmpWord.contains("如果结束")){
                        ifFlag = false;
                        break;
                    }
                    listLoop.add(tmp);
                }while((!tmpWord.contains("结束") && i < list.size()));

                ifResult.append("<title name=\"LOC\">").append(curifCMD).append("</title>").append("<statement name=\"DO\">");
                ifResult.append(convertMyXMLCodeNine(listLoop));
                ifResult.append("</statement>");

                ifCodeList.add(ifResult.toString());
                continue;
            }

        }while(i < list.size());

        for(int k = 0;k < directionList.size(); k ++ ){
            String typeWord = directionList.get(k).trim();

            if( k > 0){
                result.append("<next>");
                firstNext = firstNext + 1;
            }

            if(typeWord.contains("turnLeft")){
                result.append("<block type=\"maze_turn\"><title name=\"DIR\">turnLeft</title>");
            }else if(typeWord.contains("turnRight")){
                result.append("<block type=\"maze_turn\"><title name=\"DIR\">turnRight</title>");
            }else if(typeWord.contains("bee_ifFlower")) {
                result.append("<block type=\"bee_ifFlower\">");
                //处理IF判断逻辑
                int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                result.append(ifCodeList.get(tmpCount - 1));
            }else if(typeWord.contains("controls_repeat")){
                result.append("<block type=\"controls_repeat\">");
                //重复 xml code makeup
                int tmpCount = Integer.parseInt(typeWord.substring(typeWord.indexOf("=CG=")+"=CG=".length() ,typeWord.length()));
                result.append(loopCodeList.get(tmpCount - 1));
            }else{
                result.append("<block type=\"").append(typeWord).append("\">");
            }

        }
        result.append("</block>");
        for(int k = 0; k < firstNext;k ++){
            result.append("</next></block>");
        }

        result.append("</xml>");
    }

    private static StringBuffer convertMyXMLCodeNine(List<WordsResult> listLoop) {
        StringBuffer sb = new StringBuffer();
        List<String> directionList = new ArrayList<String>();
        int i =0;
        do{
            WordsResult dto = listLoop.get(i);
            String curWord = dto.getWords();
            i = i + 1;
            if(curWord.contains("北") || curWord.contains("上")){
                directionList.add("maze_moveNorth");
                continue;
            }
            if(curWord.contains("南") || curWord.contains("下")){
                directionList.add("maze_moveSouth");
                continue;
            }
            if(curWord.contains("西")  || curWord.contains("左")){
                directionList.add("maze_moveWest");
                continue;
            }
            if(curWord.contains("东")  || curWord.contains("右")){
                directionList.add("maze_moveEast");
                continue;
            }
            if(curWord.contains("左转")){//向左转90度
                directionList.add("turnLeft");
                continue;
            }
            if(curWord.contains("右转")){//向右转90度
                directionList.add("turnRight");
                continue;
            }
            if(curWord.contains("花朵") || curWord.contains("在花")){
                directionList.add("atFlower");
                continue;
            }
            if(curWord.contains("前")){//向前移动
                directionList.add("maze_moveForward");
                continue;
            }
            if(curWord.contains("花")){
                directionList.add("maze_nectar");
                continue;
            }
            if(curWord.contains("制作") || curWord.contains("做蜂")){
                directionList.add("maze_honey");
                continue;
            }
            if(curWord.contains("玉米")){
                directionList.add("harvester_corn");
                continue;
            }
            if(curWord.contains("宝石")){
                directionList.add("collector_collect");
                continue;
            }

        }while(i < listLoop.size());
        //组装 xml 格式
        for(int k = 0;k < directionList.size(); k ++ ){
            if( k > 0) sb.append("<next>");
            if(directionList.get(k).contains("turnLeft")){
                sb.append("<block type=\"maze_turn\"><title name=\"DIR\">turnLeft</title>");
            }else if(directionList.get(k).contains("turnRight")){
                sb.append("<block type=\"maze_turn\"><title name=\"DIR\">turnRight</title>");
            }else{
                sb.append("<block type=\"").append(directionList.get(k)).append("\">");
            }

        }
        //计算next个数
        int nextCount = search(sb.toString(),"next");
        sb.append("</block>");
        for(int k = 0; k < nextCount;k ++){
            sb.append("</next></block>");
        }

        return sb;
    }

    // 判断一个字符串是否含有数字
    public static boolean HasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }
}
